#include <assert.h>
#include "unittest1.h"

void testDiscard(int p, struct gameState *postState){
    struct gameState preState;
    int failed_tests = 0;
    srand(time(NULL));
    memcpy(&preState,postState,sizeof(struct gameState));
    int result;
    int preCount = postState->handCount[p];
    int prePlayed = postState->playedCardCount;
    printf("Number of cards in hand for player %i is %i: \n",p,postState->handCount[p]);
    //test drawing first card for player 1, trashing and incrementing played count
    result = discardCard(p,0,postState,1);
    if(result !=0){
        printf("Failure in discard for the first card for player %i\n",p);
        failed_tests+=1;
    }
    if(preCount-1!=postState->handCount[p]){
        printf("Failure to discard card correctly for player %i. without trashing.\n",p);
        failed_tests+=1;
    }
    preCount = postState->handCount[p];
    //testing same player, with last card in hand, and not trashing the card
    result = discardCard(p,(preCount-1),postState,0);
    if(result!=0 || (preCount-1!=postState->handCount[p]) ){
        printf("Failure to discard card correctly without trashing on last card.\n");
        failed_tests+=1;
    }
    if(prePlayed == postState->playedCardCount){
        printf("Failure to increment played count for when card is not trashed.\n");
        failed_tests+=1;
    }
    //testing random discard between 0 and handcount with trashing while there is more than 1 card in hand
    while(postState->handCount[p] > 1){
        printf("Number of cards in hand for player %i is %i: \n",p,postState->handCount[p]);
        preCount = (postState->handCount[p]-1);
        int rnum = rand()%preCount;
        printf("random number %i\n",rnum);
        result = discardCard(p,rnum,postState,1);
        printf("Number of cards in hand for player %i is %i: \n",p,postState->handCount[p]);
        if(preCount-1!=postState->handCount[p]){
            printf("Failed random discard on %i for player %i with trashing\n",rnum,p);
            failed_tests+=1;
        }
    }
    printf("Number of cards in hand for player %i is %i: \n",p,postState->handCount[p]);
    
    //test discard of last card
    preCount = postState->handCount[p];
    result = discardCard(0,p,postState,0);
    if(preCount==postState->handCount[p]){
        printf("Failed to discard last card\n");
        failed_tests+=1;
    }
    //test for empty hand discard
    if(postState->handCount[p]!=0){
        postState->handCount[p]=0;
    }
    result = discardCard(p,0,postState,0);
    if(result==0){
        printf("Failed test case for discard on an empty hand\n");
        failed_tests+=1;
    }
    printf("Number of cards in hand for player %i is %i: \n",p,postState->handCount[p]);
    if(failed_tests==0){
        printf("All tests for discardCard passed\n");
    }else{
        printf("Number of failed tests against discardCard: %i \n",failed_tests);
    }

}

int main(int argc, char** argv){
    int cards[10] = {adventurer, council_room, feast, gardens, mine,
        remodel, smithy, village, baron, great_hall};

    struct gameState* game =newGame();
    int initialized = initializeGame(2,cards,42,game);
    if(initialized!=-1){
        testDiscard(0,game);
    }
    return 0;
    
}